/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.NMEA;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;
import com.chaeron.GPS.exception.*;


//***************************************************************************
//*
//*     RMCSentence Class definition
//*
//***************************************************************************/

/**
 * The RMCSentence class represents NMEA RMC - Recommended Minimum Navigation Info "C"
 *
 */

public class RMCSentence extends NMEASentence implements GPSVelocity, GPSNavGeneral
{
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	private final static int		RMC_NUM_FIELDS				= 11;
	
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private float		utc							= 0.0f;
	private char		status						= 0;
	private double		latitude					= 0.0f;
	private double		longitude					= 0.0f;
	private float		speed						= 0.0f;
	private float		track						= 0.0f;
	private int			date						= 0;
	private float		variation					= 0.0f;
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	/**
	 * Create a RMCSentence from the given packet data
	 *
	 * @param	tid			talker ID
	 * @param	sid			Sentence ID
	 * @param	sentenceStr	sentence string buffer
	 */
	
	RMCSentence( int tid, int sid, String sentenceStr )
	{  
		super( tid, sid, sentenceStr );
		
		setFieldCount( RMC_NUM_FIELDS );
	}
	
	
	//***************************************************************************
	//*
	//*     Methods
	//*
	//***************************************************************************/
	
	/**
	 * Decode the data fields for a RMC sentence
	 *
	 * @return		This method will return a standard error code or GPS.ERROR_OK
	 * @throws GPSPacketException
	 * @throws GPSFunctionUnavailableException
	 */
	
	public void parseData() throws GPSPacketException, GPSFunctionUnavailableException
	{  
		int		i;
		
		if( getFieldCount() >= RMC_NUM_FIELDS ) {
			utc					= getNMEAFloat( getField( 0 ) );					// UTC Time
			status				= getNMEAChar( getField( 1 ) );						// Status
			latitude 			= getNMEALatLong( getField( 2 ), getField( 3 ) );	// Latitude (degrees)
			longitude			= getNMEALatLong( getField( 4 ), getField( 5 ) );	// Longitude (degrees)
			speed				= getNMEAFloat( getField( 6 ) );					// Speed (knots)
			track				= getNMEAFloat( getField( 7 ) );					// Track made good, (degrees true)
			date				= getNMEAInt( getField( 8 ) );						// Date (ddmmyy)
			variation			= getNMEADegrees( getField( 9 ), getField( 10 ) );	// Magnetic Variation (degrees)			
		} else {
			throw( new GPSPacketException( GPSPacketException.EXCEPTION_NUMFIELDS_INVALID ) );
		}
	}
	
	
	//***************************************************************************
	//*
	//*     Sentence Processing Methods
	//*
	//***************************************************************************/
	
	/**
	 * Process the sentence
	 *
	 * @param	gps			the GPS object that requested the RMC sentence to be processed
	 */
	
	void process( GPS gps )
	{
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_VELOCITY, this ) );
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_NAVIGATION_GENERAL, this ) );
	}
	
	
	/**
	 * Get events generated.
	 *
	 * @return		return a valid event type(s) that will be generated by this sentence subclass
	 */
	
	static int getEventsGenerated()
	{  
		return( GPSEvent.GPS_EVENT_VELOCITY | GPSEvent.GPS_EVENT_NAVIGATION_GENERAL | GPSEvent.GPS_EVENT_NMEA_SENTENCE );
	}
	
	
	/**
	 * Does the object contain valid data?
	 *
	 * @return		Does the object contain valid data?
	 */
	
	public boolean isDataValid()
	{  
		return( utc != 0.0f || latitude != 0.0f || longitude != 0.0f || speed != 0.0f || track != 0.0f || variation != 0.0f );
	}
	
	
	//***************************************************************************
	//*
	//*    Accessor methods
	//*
	//***************************************************************************/
	
	/**
	 * Get the UTC time
	 *
	 * @return		UTC time
	 */
	
	public float getUTC()
	{  
		return( utc );
	}
	
	
	/**
	 * Get the Status
	 *
	 * @return		status
	 */
	
	public char getStatus()
	{  
		return( status );
	}
	
	
	/**
	 * Get the Latitude (degrees)
	 *
	 * @return		Latitude (degrees)
	 */
	
	public double getLatitude()
	{  
		return( latitude );
	}
	
	
	/**
	 * Get the Longitude (degrees)
	 *
	 * @return		Longitude (degrees)
	 */
	
	public double getLongitude()
	{  
		return( longitude );
	}
	
	
	/**
	 * Get the Speed (knots)
	 *
	 * @return		Speed (knots)
	 */
	
	public float getSpeed()
	{  
		return( speed );
	}
	
	
	/**
	 * Get the Track made Good (degrees true)
	 *
	 * @return		Track made Good (degrees true)
	 */
	
	public float getTrackMadeGood()
	{  
		return( track );
	}
	
	
	/**
	 * Get the Date (ddmmyy)
	 *
	 * @return		Date (ddmmyy)
	 */
	
	public int getDate()
	{  
		return( date );
	}
	
	
	/**
	 * Get the Magnetic Variation (degrees)
	 *
	 * @return		Magnetic Variation (degrees)
	 */
	
	public float getMagneticVariation()
	{  
		return( variation );
	}
	
	
	/**
	 * Get the Velocity
	 *
	 * @return		Velocity (meters/sec)
	 */
	
	public double getVelocity()
	{  
		return( GPS.convertKnotsToMetersSec( speed ) );
	}
	
	
	/**
	 * Get the Velocity East
	 *
	 * @return		Velocity  East (meters/sec)
	 */
	
	public float getVelocityEast()
	{  
		return( 0.0f );
	}
	
	
	/**
	 * Get the Velocity North
	 *
	 * @return		Velocity  North (meters/sec)
	 */
	
	public float getVelocityNorth()
	{  
		return( 0.0f );
	}
	
	
	/**
	 * Get the Velocity Up
	 *
	 * @return		Velocity  Up (meters/sec)
	 */
	
	public float getVelocityUp()
	{  
		return( 0.0f );
	}
	
}
